package com.jswlw.controller.admin.biz;

import com.github.pagehelper.Page;
import com.jswlw.common.BaseException;
import com.jswlw.common.ResponseStatus;
import com.jswlw.controller.BaseController;
import com.jswlw.entity.biz.DistributionAccounts;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.service.biz.DistributionAccountsService;
import com.jswlw.service.biz.DistributorService;
import com.jswlw.vo.DistributionAccountsVo;
import com.jswlw.web.manager.SessionAdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

@Controller
@RequestMapping("/admin/biz/distributionAccounts")
public class AdminDistributionAccountsController extends BaseController {

    @Autowired
    DistributionAccountsService service;
    @Autowired
    DistributorService distributorService;

    @RequestMapping("/index")
    public String index(Model model) {
        return "biz/distributionAccounts/index";
    }

    /**
     * 广告列表
     *
     * @param
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody DistributionAccounts distributionAccounts) {
        if (Objects.nonNull(SessionAdminManager.getCurrentUser().getDistributor())) {
            distributionAccounts.setDistributorIds(SessionAdminManager.getCurrentUser().getDistributor().getChildrenIds());
        }
        Page<DistributionAccounts> page = service.queryPage(distributionAccounts);
        return pageResult(page.getResult(), page.getTotal());
    }

    /**
     * 新增广告
     *
     * @return
     */
    @RequestMapping("/add/{distributorId}")
    public ModelAndView add(@PathVariable("distributorId") Integer distributorId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("biz/distributionAccounts/edit");
        modelAndView.addObject("distributorId", distributorId);
        return modelAndView;
    }

    /**
     * 编辑广告
     *
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("info", service.find4Id(id));
        modelAndView.setViewName("biz/distributionAccounts/edit");
        return modelAndView;
    }

    /**
     * 保存广告
     *
     * @param distributionAccounts
     * @param
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(DistributionAccountsVo distributionAccounts) {
        Distributor distributor = distributorService.find4Id(distributionAccounts.getDistributorId());
        if (Objects.isNull(distributor)) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "经销商不存在");
        }

        if (distributionAccounts.getAmount().equals(0)) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "结算金额未0");
        }
        try {
            if (distributionAccounts.getId() == null) {
                service.add(distributionAccounts.toDistributionAccounts());
            } else {
                service.update(distributionAccounts.toDistributionAccounts());
            }
            return this.successResult(null);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }

    /**
     * 删除广告
     *
     * @param
     * @param id
     * @return
     */
    @RequestMapping("/accounts")
    @ResponseBody
    public ModelMap accounts(Integer id) {
        try {
            service.accounts(id);
            return this.successResult("删除成功");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("删除失败" + e.getMessage());
        }
    }
}
